package $___666;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;


class UnionFind {
    int[] parent;
    int[] size; // 根结点对应的分量的大小
    int count; // 连通分量的数量

    public UnionFind(int n) {
        parent = new int[n];
        size = new int[n];
        for (int i = 0; i < n; i++) {
            parent[i] = i;
        }
        for (int i = 0; i < n; i++) {
            size[i] = 1;
        }
        count = n;
    }

    int find(int p) {
        while (parent[p] != p) {
            p = parent[p];
        }
        return p;
    }

    boolean isConnected(int p, int q) {
        return find(p) == find(q);
    }

    void union(int p, int q) {
        int pRoot = find(p);
        int qRoot = find(q);
        if (pRoot == qRoot) {
            return;
        }
        // 让小的指向大的
        if (size[pRoot] < size[qRoot]) {
            parent[pRoot] = qRoot;
            size[qRoot] += size[pRoot];
        }
        else {
            parent[qRoot] = pRoot;
            size[pRoot] += size[qRoot];
        }
        count--;
    }
}

/**
 * @author wangshaoyu
 */
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(); // n个村庄
        int m = in.nextInt(); // m条公路
        int[][] arr = new int[m][3];
        for (int i = 0; i < m; i++) {
            arr[i][0] = in.nextInt() - 1;
            arr[i][1] = in.nextInt() - 1;
            arr[i][2] = in.nextInt(); // 修好的时间
        }
        // 将修复时间从小到大进行排序
        Arrays.sort(arr, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[2] - o2[2];
            }
        });
        int ans = 0;
        int i = 0;
        UnionFind uf = new UnionFind(n);
        for (; i < m; i++) {
            uf.union(arr[i][0], arr[i][1]);
            ans = arr[i][2];
            if (uf.count == 1) {
                break;
            }
        }
        if (i < m) {
            System.out.println(ans);
        }
        else {
            System.out.println(-1);
        }
    }
}
